package com.dcy.seataaccount.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dcy.common.annotation.Log;
import com.dcy.common.model.PageModel;
import com.dcy.common.model.R;
import com.dcy.common.model.PageResult;
import com.dcy.seataaccount.biz.dto.input.AccountTblCreateInputDTO;
import com.dcy.seataaccount.biz.dto.input.AccountTblSearchInputDTO;
import com.dcy.seataaccount.biz.dto.input.AccountTblUpdateInputDTO;
import com.dcy.seataaccount.biz.dto.output.AccountTblListOutputDTO;
import com.dcy.seataaccount.biz.dtomapper.MAccountTblMapper;
import com.dcy.seataorder.api.model.OrderTbl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.dcy.seataaccount.api.model.AccountTbl;
import com.dcy.seataaccount.biz.service.AccountTblService;
import org.springframework.web.bind.annotation.*;
import com.dcy.db.base.controller.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dcy
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/account-tbl")
@Api(value = "AccountTblController", tags = {"操作接口"})
public class AccountTblController extends BaseController<AccountTblService,AccountTbl> {

    @Autowired
    private MAccountTblMapper mAccountTblMapper;

    @Log
    @ApiOperation(value = "字典类型分页查询")
    @GetMapping("/page")
    public R<PageResult<AccountTblListOutputDTO>> pageList(AccountTblSearchInputDTO accountTblSearchInputDTO, PageModel pageModel) {
        // 转换model
        AccountTbl accountTbl = mAccountTblMapper.toAccountTbl(accountTblSearchInputDTO);
        // 获取源对象
        IPage<AccountTbl> pageListByListInputDTO = baseService.pageListByEntity(accountTbl,pageModel);
        // 转换新对象
        List<AccountTblListOutputDTO> accountTblListOutputDTOS = mAccountTblMapper.toList(pageListByListInputDTO.getRecords());
        // 返回业务分页数据
        return success(toPageDTO(pageListByListInputDTO, accountTblListOutputDTOS));
    }

    @Log
    @ApiOperation(value = "添加")
    @PostMapping("/save")
    public R<Boolean> save(@ApiParam @RequestBody AccountTblCreateInputDTO accountTblCreateInputDTO) {
        AccountTbl accountTbl = mAccountTblMapper.toAccountTbl(accountTblCreateInputDTO);
        return super.save(accountTbl);
    }

    @Log
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public R<Boolean> update(@ApiParam @RequestBody AccountTblUpdateInputDTO accountTblUpdateInputDTO) {
        AccountTbl accountTbl = mAccountTblMapper.toAccountTbl(accountTblUpdateInputDTO);
        return super.update(accountTbl);
    }

    @Log
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    @PostMapping(value = "/delete")
    public R<Boolean> delete(@RequestParam String id) {
        return super.delete(id);
    }

    @Log
    @ApiOperation(value = "根据list删除")
    @PostMapping(value = "/deleteBatch")
    public R<Boolean> deleteBatch(@ApiParam @RequestBody List<String> idList) {
        return super.deleteBatch(idList);
    }

    @Log
    @ApiOperation(value = "采购")
    @PostMapping(value = "/purchase")
    public R<OrderTbl> purchase(String userId, String commodityCode, int orderCount,int ex) {
        // C201901140001
        return success(baseService.purchase(userId,commodityCode,orderCount,ex));
    }
}
